/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8                               :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2025 Justine Alexandra Roberts Tunney                              │
│                                                                              │
│ Permission to use, copy, modify, and/or distribute this software for         │
│ any purpose with or without fee is hereby granted, provided that the         │
│ above copyright notice and this permission notice appear in all copies.      │
│                                                                              │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             │
│ PERFORMANCE OF THIS SOFTWARE.                                                │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/proc/proc.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/temp.h"
#include "libc/testlib/testlib.h"

TEST(vfork, exit) {
  int pid = vfork();
  EXPECT_NE(-1, pid);
  if (!pid)
    _Exit(23);
  int ws;
  EXPECT_EQ(pid, wait(&ws));
  EXPECT_TRUE(WIFEXITED(ws));
  EXPECT_EQ(23, WEXITSTATUS(ws));
}

TEST(vfork, crash) {
  if (IsLinux())
    return;  // todo: why does this break on Linux pi 5.15.0-1076-raspi
  int pid = vfork();
  EXPECT_NE(-1, pid);
  if (!pid) {
    signal(SIGILL, SIG_DFL);
    notpossible;
  }
  int ws;
  EXPECT_EQ(pid, waitpid(pid, &ws, 0));
  EXPECT_TRUE(WIFSIGNALED(ws));
  EXPECT_EQ(SIGILL, WTERMSIG(ws));
}

TEST(vfork, file) {
  char path[] = "/tmp/vfork_test.XXXXXX";
  ASSERT_SYS(0, 3, mkstemp(path));
  int pid = vfork();
  EXPECT_NE(-1, pid);
  if (!pid) {
    EXPECT_SYS(0, 4, open(path, O_RDONLY));
    EXPECT_SYS(0, 5, dup(3));
    EXPECT_SYS(0, 6, dup(4));
    _Exit(23);
  }
  int ws;
  EXPECT_EQ(pid, waitpid(pid, &ws, 0));
  EXPECT_TRUE(WIFEXITED(ws));
  EXPECT_EQ(23, WEXITSTATUS(ws));
  EXPECT_SYS(0, 4, open(path, O_RDONLY));
  EXPECT_SYS(0, 0, close(3));
  EXPECT_SYS(0, 0, close(4));
  EXPECT_SYS(0, 0, unlink(path));
}

TEST(vfork, urandom) {
  int pid = vfork();
  EXPECT_NE(-1, pid);
  if (!pid) {
    EXPECT_SYS(0, 3, open("/dev/urandom", O_RDONLY));
    _Exit(23);
  }
  int ws;
  EXPECT_EQ(pid, waitpid(pid, &ws, 0));
  EXPECT_TRUE(WIFEXITED(ws));
  EXPECT_EQ(23, WEXITSTATUS(ws));
  EXPECT_SYS(0, 3, open("/dev/urandom", O_RDONLY));
  EXPECT_SYS(0, 0, close(3));
}

TEST(vfork, null) {
  EXPECT_SYS(0, 3, open("/dev/null", O_RDONLY));
  int pid = vfork();
  EXPECT_NE(-1, pid);
  if (!pid) {
    EXPECT_SYS(0, 4, open("/dev/null", O_RDONLY));
    EXPECT_SYS(0, 0, close(3));
    EXPECT_SYS(0, 0, close(4));
    _Exit(23);
  }
  int ws;
  EXPECT_EQ(pid, waitpid(pid, &ws, 0));
  EXPECT_TRUE(WIFEXITED(ws));
  EXPECT_EQ(23, WEXITSTATUS(ws));
  EXPECT_SYS(0, 4, open("/dev/null", O_RDONLY));
  EXPECT_SYS(0, 0, close(3));
  EXPECT_SYS(0, 0, close(4));
}
